package com.hmall.cart.listener;

import com.hmall.cart.service.ICartService;
import com.hmall.common.constants.MqConstants;
import com.hmall.common.domain.CartClearMessage;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * @author zzw
 * @date 2025/6/21
 * @description 清理购物车，消费mq消息
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class CartClearListener {

    private final ICartService cartService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(MqConstants.CART_CLEAR_QUEUE),
            exchange = @Exchange(MqConstants.TRADE_EXCHANGE_NAME),
            key = MqConstants.ORDER_CREATE_KEY
    )
    )
    public void clearCart(CartClearMessage message) {
        log.info("开始清理用户{}的购物车", message.getUserId());
        cartService.removeByItemIds(message.getUserId(), message.getItemIds());
    }
}
